//
//  HomeController.m
//  TimeTallyDemo
//
//  Created by gongwenkai on 2017/1/4.
//  Copyright © 2017年 gongwenkai. All rights reserved.
//

#import "HomeController.h"
#import "AddTallyViewController.h"
#import "TimeLineView.h"

#define kiPhoneX ([UIScreen mainScreen].bounds.size.height >= 812)

@interface HomeController ()<TimeLineViewDelegate>
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)NSDictionary *timeLineModelsDict;
@property(nonatomic,assign)CGFloat allDateAllLine;
@property (weak, nonatomic) IBOutlet UILabel *incomLab;
@property (weak, nonatomic) IBOutlet UILabel *expenseLab;
@property (weak, nonatomic) IBOutlet UIButton *bottomButton;

@end
@implementation HomeController



- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, (kiPhoneX?88:64)+100, self.view.frame.size.width, self.view.frame.size.height-(kiPhoneX?88:64)-100-(kiPhoneX?80+34:80))];
        _scrollView.backgroundColor = [UIColor whiteColor];
    }
    return _scrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",NSHomeDirectory());
    self.title = @"我的账本";
    [self.view addSubview:self.scrollView];
    
}

- (IBAction)passwordBtnAction:(id)sender {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请设置密码" message:@"密码为计算结果例如1314 解锁时输入657 X 2 = 即可解锁" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *textField = alertController.textFields.firstObject;
        NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
        [userDefa setObject:textField.text forKey:@"kPassword"];
        [userDefa synchronize];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    //定义第一个输入框；
    [alertController
     addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
         textField.placeholder = @"请输入需要设置的密码";
         textField.keyboardType = UIKeyboardTypeDecimalPad;
         //        textField.delegate = self;
     }];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self.navigationController presentViewController:alertController animated:YES completion:nil];
}

//增加一条账单
- (IBAction)clickAddTally:(id)sender {
    AddTallyViewController *addVC = [[AddTallyViewController alloc] init];
    [self.navigationController pushViewController:addVC animated:YES];
}

//出现时 刷新整个时间线
- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"will");
    [self showTimeLineView];
 
}

- (void)showTimeLineView {
    //先读取数据库中内容 封装成字典
    [self readSqliteData];
    
    //移除上一个timelineView
    for (UIView *view in self.scrollView.subviews) {
        if (view.tag == 1990) {
            [view removeFromSuperview];
        }
    }
    
    //计算总收入 和 总支出
    double incomeTotal = 0;
    double expenseTotal = 0;
    self.allDateAllLine = 0;
    //计算时间线长度
    for (NSString *key  in self.timeLineModelsDict.allKeys) {
        NSArray<TimeLineModel*> *modelArray = self.timeLineModelsDict[key];
        //一天的时间线总长
        self.allDateAllLine = self.allDateAllLine + kDateWidth + (kBtnWidth+kLineHeight)*modelArray.count + kLineHeight;
        for (TimeLineModel *model in modelArray) {
            incomeTotal = incomeTotal + model.income;
            expenseTotal = expenseTotal + model.expense;
        }
        
        for (TimeLineModel *item in modelArray) {
            if ([self MatchLetter:item.tallyIconName]) {
                self.bottomButton.hidden = NO;
                break;
            }else {
                self.bottomButton.hidden = YES;
            }
        }
    }
    
    self.incomLab.text = [NSString stringWithFormat:@"%.2f",incomeTotal];
    self.expenseLab.text = [NSString stringWithFormat:@"%.2f",expenseTotal];
    
    //设置时间线视图timelineview
    CGRect rect  = CGRectMake(0, 0, self.view.frame.size.width, self.allDateAllLine);
    TimeLineView *view = [[TimeLineView alloc] initWithFrame:rect];
    view.tag = 1990;
    view.delegate = self;
    view.backgroundColor = [UIColor whiteColor];
    view.timeLineModelsDict = self.timeLineModelsDict;
    
    [self.scrollView addSubview:view];
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.allDateAllLine);
    //滚动到顶端
    [self.scrollView setContentOffset:CGPointZero animated:YES];
    
}

-(BOOL)MatchLetter:(NSString *)str
{
    //判断是否以字母开头
    NSString *ZIMU = @"^[A-Za-z]+$";
    NSPredicate *regextestA = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", ZIMU];
    
    if ([regextestA evaluateWithObject:str] == YES)
        return YES;
    else
        return NO;
}

//读取数据库中的数据  以字典的形式 key：@"日期" object：[账单信息]
- (void)readSqliteData{
    self.timeLineModelsDict = nil;
    self.timeLineModelsDict = [[CoreDataOperations sharedInstance] getAllDataWithDict];
}

//删除前的确认
- (void)willDeleteCurrentTallyWithIdentity:(NSString*)identity {
    
    UIAlertController *alertVC =[UIAlertController alertControllerWithTitle:@"提示" message:@"确认删除" preferredStyle:UIAlertControllerStyleAlert ];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //数据库操作：从数据库中删除 Tally表中对应identity字段行
        Tally *tally = [[CoreDataOperations sharedInstance] getTallyWithIdentity:identity];
        [[CoreDataOperations sharedInstance] deleteTally:tally];
        //删除完成后 刷新视图
        [self showTimeLineView];

    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alertVC animated:YES completion:nil];

}

//TimeLineViewDelegate  delegate 修改前的准备
- (void)willModifyCurrentTallyWithIdentity:(NSString*)identity {
    AddTallyViewController *addVC = [[AddTallyViewController alloc] init];
    [addVC selectTallyWithIdentity:identity];
    [self.navigationController pushViewController:addVC animated:YES];

}
@end
